import confg from './config'
const {
  Member
} = require('./api')
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 时间戳转化为年 月 日 时 分 秒
 * ts: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
function timeFormat(timestamp, format) {
  const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  let returnArr = [];
  let date = new Date(timestamp);
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  returnArr.push(year, month, day, hour, minute, second);
  returnArr = returnArr.map(formatNumber);
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
const updataImg = cb => {
  wx.chooseImage({
    success: (succ) => {
      //向服务器端上传图片 
      wx.uploadFile({
        url: confg.base_url + '/api/v1/image/upload',
        filePath: succ.tempFilePaths[0],
        name: 'file',
        formData: {},
        success: function (res) {
          cb(JSON.parse(res.data))
        },
        fail: function (err) {
          console.log(err)
        }
      });
    }
  })
}
const timeTest = str => {
  let t = new Date().getTime() - new Date(str).getTime(),
    mm = 60 * 1000,
    hh = 60 * mm,
    dd = 24 * hh,
    text = '';
  if (t > dd) {
    text = parseInt(t / dd) + '天前发布'
  } else if (t > hh) {
    text = parseInt(t / hh) + '小时前发布'
  } else if (t > mm) {
    text = parseInt(t / mm) + '分钟前发布'
  } else {
    text = '1分钟前发布'
  }
  return text
}
const checkLogin = (cb, type, level) => {
  wx.request({
    url: confg.base_url + '/api/v1/member',
    data: {},
    method: 'POST',
    header: {
      'content-type': 'application/json',
      'authorization': wx.getStorageSync('token_')
    },
    success: function (re) { //服务器返回数据
      let res = re.data,user = res.data;
      if (res.code == 200) {
        user.type ? user.type = user.type : user.type = 0;
        console.log(type,user.type)
        if (type == null || user.type == type) {
          cb(user)
        } else if(user.type==0){
          wx.showModal({
            title: '提示',
            content: confg.auth_text[user.type],
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/postcard/postcard',
                })
              }
            }
          })
        }else {
          wx.showModal({
            title: '提示',
            content: confg.auth_text[type],
            showCancel: false
          })
        }
      } else {
        wx.removeStorage({
          key: 'user'
        })
        // 没有登录
        wx.showModal({
          title: '提示',
          content: '您还没有登录，请登录!',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/my/my',
              })
            }
          }
        })
      }
    },
    error: function (e) {

    }
  })

}
module.exports = {
  timeFormat: timeFormat,
  updataImg: updataImg,
  timeTest: timeTest,
  checkLogin: checkLogin
}